package com.hfkh.keyun.controller.backend;
import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Carousel;
import com.hfkh.keyun.exception.BusinessException;
import com.hfkh.keyun.service.CarouselService;
import com.hfkh.keyun.vo.CarouselVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
@RestController
@RequestMapping("/api/carousel")
@Api(tags = "轮播图管理")
public class CarouselController {
    @Resource
    private CarouselService carouselService;
    @ApiOperation("按id删除轮播列表")
    @PostMapping("/deleteCarousel")
    public ApiResult deleteCarousel(@RequestBody Integer id) {
              carouselService.removeById(id);
            return ApiResult.ok("删除轮播图成功", "头像上传成功！");
    }
    /*
 paramType:
  1、@RequestParam     paramType="query"
  2、@PathVariable     paramType="path"
  3、 @RequestBody     paramType="body/form"
      */
    @ApiOperation("按标题分页查询轮播列表")
    @GetMapping("/queryPage")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "imgTitle",value = "标题",required = false,paramType = "query",dataType = "String",defaultValue = ""),
                    @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query",dataType = "Integer",defaultValue = "1"),
                    @ApiImplicitParam(name = "size",value = "页容量",required = true,paramType = "query",dataType = "Integer",defaultValue = "10")
            }
    )
    public ApiResult queryPage(
            @RequestParam(required = false,defaultValue = "") String imgTitle, @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10") Integer size){
        return carouselService.queryPage(imgTitle, page, size);
    }


    @ApiOperation("新增轮播图")
    @PostMapping("/addCarousel")
    public ApiResult addCarousel(CarouselVo carouselVo) throws BusinessException, IOException {
        return  carouselService.addCarousel(null,carouselVo);
    }

    @ApiOperation("修改轮播图")
    @PostMapping("/updateCarousel")
    public ApiResult updateCarousel(Integer id, CarouselVo carouselVo) throws BusinessException, IOException {
        return  carouselService.addCarousel(id,carouselVo);
    }

    @ApiOperation("根据id获取轮播图详情")
    @GetMapping("/queryCarousel")
    public ApiResult queryCarousel(Integer id){
        Carousel byId = carouselService.getById(id);
        return ApiResult.success(byId);
    }


}
